<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page import="com.hp.hpl.jena.rdf.model.ResIterator" %>
<%@ page import="com.hp.hpl.jena.vocabulary.RDFS" %>
<%@ page import="com.hp.hpl.jena.rdf.model.Model" %>
<%@ page import="com.hp.hpl.jena.rdf.model.Resource" %>
<%@ page import="com.hp.hpl.jena.vocabulary.OWL" %>
<%@ page import="com.hp.hpl.jena.vocabulary.RDF" %>
<%@ page import="jenajdo.JenaJDOFactory" %>
<%@ page import="thanantic.ThananticUtil" %>

<html>
<head>
	<title>Semantic CRUD</title>
	<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
	<script type="text/javascript">function checkText(textBox, errorStar) {
		var isValid = textBox.value != "";
		if (! isValid)
			errorStar.style.visibility = "visible";
		return isValid;
	}</script>
</head>
<body>
	<%
		String errorMessage = request.getParameter("errorMessage");
		String className = request.getParameter("ontclass");
		Model model = JenaJDOFactory.createModelJDO();
		if (className == null)
			className = OWL.Thing.toString();
		Resource ontClass = model.getResource(className);
		Resource parent = ontClass.getPropertyResourceValue(RDFS.subClassOf);
		if (errorMessage != null) {
	%>
		<div class="errorText"><%= errorMessage %></div>
	<% } %>
	<h1><%= ThananticUtil.getLabel(ontClass) %></h1>
	<div>
		Navigation:
		<a href="/crud.jsp?ontclass=<%= ThananticUtil.getURI(parent) %>"><%= ThananticUtil.getLabel(parent) %></a>
		<select onchange="window.location = this.options[this.selectedIndex].value">
			<option>Child Class</option>
		<% 
			ResIterator children = model.listSubjectsWithProperty(RDFS.subClassOf, ontClass);
			while (children.hasNext()) {
				Resource child = children.next();
		%>
			<option value="/crud.jsp?ontclass=<%= ThananticUtil.getURI(child) %>">
				<%= ThananticUtil.getLabel(child) %>
			</option>
		<% } %></select>
		<a href="\query.jsp">Querier</a>
	</div>
	<form action="/class" method="post">
		<input type="hidden" name="inClass" value="<%= ontClass.toString() %>" />
		<input id="cText" type="text" name="cLabel" />
		<div class="errorStar" id="cErrorStar">*</div>
		<input type="submit" name="cSubmit" value="New Class" onclick="return checkText()" />
		<input type="submit" name="cSubmit" value="Delete Class"
			onclick="return confirm('Are you sure you want to delete this class?')" />
		<input type="submit" name="cSubmit" value="New Property"
			onclick="return checkText(document.getElementById('cText'), document.getElementById('cErrorStar'))" />
	</form>
	<h4>Properties:</h4>
	<%
		ResIterator objClasses = model.listSubjectsWithProperty(RDF.type, OWL.Class);
		ResIterator properties = model.listSubjectsWithProperty(RDFS.domain, ontClass);
		while (properties.hasNext()) {
			Resource property = properties.next();
			Resource domain = property.getPropertyResourceValue(RDFS.domain);
	%><form action="/property" method="post">
		<input type="hidden" name="inProperty" value="<%= property.toString() %>" />
		<input id="pText" type="text" name="pLabel" />
		<div class="errorStar" id="pErrorStar">*</div>
		<select name="dataType">
			<option>object</option>
			<option>string</option>
			<option>boolean</option>
			<option>decimal</option>
			<option>float</option>
			<option>double</option>
			<option>dateTime</option>
			<option>time</option>
			<option>date</option>
		</select>
		<select name="objectClass"><%
			while (objClasses.hasNext()) {
				Resource objClass = objClasses.next();
		%>
			<option value="<%= ThananticUtil.getURI(objClass) %>"><%= ThananticUtil.getLabel(objClass) %></option>
		<% } %></select>
		<input type="checkbox" name="isFunctional">Functional</input>
	</form><% } %>
	<% model.close(); %>
</body>
</html>
